<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="商品编号" prop="goodsNo">
        <el-input
          v-model="queryParams.goodsNo"
          placeholder="请输入商品编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="商品所属类目ID" prop="categoryId">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入商品所属类目ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商ID" prop="supplierId">
        <el-input
          v-model="queryParams.supplierId"
          placeholder="请输入供应商ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提货地址(自提点)" prop="teamId">
        <el-input
          v-model="queryParams.teamId"
          placeholder="请输入提货地址(自提点)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配送时间" prop="deliveryTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.deliveryTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择配送时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="商品关键字，采用逗号间隔" prop="keywords">
        <el-input
          v-model="queryParams.keywords"
          placeholder="请输入商品关键字，采用逗号间隔"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品图片" prop="picUrl">
        <el-input
          v-model="queryParams.picUrl"
          placeholder="请输入商品图片"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品简介" prop="brief">
        <el-input
          v-model="queryParams.brief"
          placeholder="请输入商品简介"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否上架" prop="isOnSale">
        <el-input
          v-model="queryParams.isOnSale"
          placeholder="请输入是否上架"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品分享朋友圈图片" prop="shareUrl">
        <el-input
          v-model="queryParams.shareUrl"
          placeholder="请输入商品分享朋友圈图片"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="零售价格" prop="retailPrice">
        <el-input
          v-model="queryParams.retailPrice"
          placeholder="请输入零售价格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠价格" prop="favorablePrice">
        <el-input
          v-model="queryParams.favorablePrice"
          placeholder="请输入优惠价格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品详细介绍，是富文本格式" prop="detail">
        <el-input
          v-model="queryParams.detail"
          placeholder="请输入商品详细介绍，是富文本格式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="浏览量" prop="browse">
        <el-input
          v-model="queryParams.browse"
          placeholder="请输入浏览量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已销售总量" prop="sales">
        <el-input
          v-model="queryParams.sales"
          placeholder="请输入已销售总量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="佣金类型" prop="brokerageType">
        <el-select v-model="queryParams.brokerageType" placeholder="请选择佣金类型" clearable size="small">
          <el-option
            v-for="dict in brokerageTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="推广佣金金额" prop="brokeragePrice">
        <el-input
          v-model="queryParams.brokeragePrice"
          placeholder="请输入推广佣金金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存" prop="inventory">
        <el-input
          v-model="queryParams.inventory"
          placeholder="请输入库存"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销量" prop="saleCount">
        <el-input
          v-model="queryParams.saleCount"
          placeholder="请输入销量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运费" prop="freight">
        <el-input
          v-model="queryParams.freight"
          placeholder="请输入运费"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:goods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:goods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:goods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:goods:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" size="small" border fit highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form label-position="left" class="table-expand">
            <el-form-item label="宣传画廊">
              <img v-for="pic in scope.row.banners" :key="pic" :src="pic" class="banners">
            </el-form-item>
            <el-form-item label="商品介绍">
              <span>{{ scope.row.brief }}</span>
            </el-form-item>
            <el-form-item label="商品单位">
              <span>{{ scope.row.specifications }}</span>
            </el-form-item>
            <el-form-item label="关键字">
              <span>{{ scope.row.keywords }}</span>
            </el-form-item>
            <el-form-item label="类目ID">
              <span>{{ scope.row.categoryId }}</span>
            </el-form-item>
            <el-form-item label="供应商ID">
              <span>{{ scope.row.supplierId }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>

      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="ID" align="center" prop="goodsId" />
      <!--<el-table-column label="商品编号" align="center" prop="goodsNo" />-->
      <el-table-column label="名称" align="center" min-width="100" prop="name" />
      <!--<el-table-column label="商品所属类目ID" align="center" prop="categoryId" />
      <el-table-column label="供应商ID" align="center" prop="supplierId" />
      <el-table-column label="提货地址(自提点)" align="center" prop="teamId" />
      <el-table-column label="配送时间" align="center" prop="deliveryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品关键字，采用逗号间隔" align="center" prop="keywords" />-->
      <el-table-column label="商品图片" align="center" prop="picUrl">
        <template slot-scope="scope">
          <img :src="scope.row.picUrl" width="40">
        </template>
      </el-table-column>
      <!--<el-table-column label="商品banner图，采用JSON数组格式" align="center" prop="banners" />
      <el-table-column label="商品简介" align="center" prop="brief" />-->
      <el-table-column label="是否上架" align="center" prop="isOnSale" :formatter="isOnSaleFormat" />
      <!--<el-table-column label="商品分享朋友圈图片" align="center" prop="shareUrl" />
      <el-table-column label="商品规格" align="center" prop="specifications" />-->
      <el-table-column label="零售价格" align="center" prop="retailPrice" />
      <el-table-column label="优惠价格" align="center" prop="favorablePrice" />
      <el-table-column label="详情" align="center" prop="detail">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="showDetail(scope.row)"
            v-hasPermi="['module:orders:query']"
          >查看</el-button>
          <el-dialog :visible.sync="openDetail" title="商品详情">
            <div v-html="form.detail"/>
          </el-dialog>
        </template>
      </el-table-column>
      <!--<el-table-column label="浏览量" align="center" prop="browse" />
      <el-table-column label="已销售总量" align="center" prop="sales" />
      <el-table-column label="佣金类型" align="center" prop="brokerageType" :formatter="brokerageTypeFormat" />
      <el-table-column label="推广佣金金额" align="center" prop="brokeragePrice" />
      <el-table-column label="库存" align="center" prop="inventory" />
      <el-table-column label="销量" align="center" prop="saleCount" />
      <el-table-column label="运费" align="center" prop="freight" />
      <el-table-column label="排序" align="center" prop="sort" />-->
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="是否推荐" align="center" prop="isRecommend">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isRecommend"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:goods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:goods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods, exportGoods, recommendGoods } from "@/api/module/goods";

export default {
  name: "Goods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 商品表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否上架字典
      isOnSaleOptions: [],
      // 佣金类型字典
      brokerageTypeOptions: [],
      // 状态字典
      statusOptions: [],
      // 详情数据弹出层
      openDetail: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsNo: undefined,
        name: undefined,
        categoryId: undefined,
        supplierId: undefined,
        teamId: undefined,
        deliveryTime: undefined,
        keywords: undefined,
        picUrl: undefined,
        banners: undefined,
        brief: undefined,
        isOnSale: undefined,
        shareUrl: undefined,
        specifications: undefined,
        retailPrice: undefined,
        favorablePrice: undefined,
        detail: undefined,
        browse: undefined,
        sales: undefined,
        brokerageType: undefined,
        brokeragePrice: undefined,
        inventory: undefined,
        saleCount: undefined,
        freight: undefined,
        sort: undefined,
        isRecommend: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsNo: [
          { required: true, message: "商品编号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        deliveryTime: [
          { required: true, message: "配送时间不能为空", trigger: "blur" }
        ],
        specifications: [
          { required: true, message: "商品规格不能为空", trigger: "blur" }
        ],
        browse: [
          { required: true, message: "浏览量不能为空", trigger: "blur" }
        ],
        sales: [
          { required: true, message: "已销售总量不能为空", trigger: "blur" }
        ],
        brokerageType: [
          { required: true, message: "佣金类型不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getTDictDatas("module_yes_no").then(response => {
      this.isOnSaleOptions = response.data;
    });
    this.getTDictDatas("module_brokerage_type").then(response => {
      this.brokerageTypeOptions = response.data;
    });
    this.getTDictDatas("module_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;

        this.goodsList.forEach((goods, i) => {
          goods.banners = JSON.parse(goods.banners);
          if (goods.detail != null && goods.detail != '') {
            goods.detail = this.Base64.decode(goods.detail);
          }
        });
      });
    },
    // 是否上架字典翻译
    isOnSaleFormat(row, column) {
      return this.selectDictLabel(this.isOnSaleOptions, row.isOnSale);
    },
    // 佣金类型字典翻译
    brokerageTypeFormat(row, column) {
      return this.selectDictLabel(this.brokerageTypeOptions, row.brokerageType);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 设置商品是否推荐
    handleStatusChange(row) {
      let text = row.isRecommend === 1 ? "推荐" : "撤销";
      this.$confirm('确认要"' + text + '""' + row.name + '"商品吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return recommendGoods(row.goodsId, row.isRecommend);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function() {
        row.isRecommend = row.isRecommend === 1 ? 0 : 1;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goodsId: undefined,
        goodsNo: undefined,
        name: undefined,
        categoryId: undefined,
        supplierId: undefined,
        teamId: undefined,
        deliveryTime: undefined,
        keywords: undefined,
        picUrl: undefined,
        banners: undefined,
        brief: undefined,
        isOnSale: undefined,
        shareUrl: undefined,
        specifications: undefined,
        retailPrice: undefined,
        favorablePrice: undefined,
        detail: undefined,
        browse: undefined,
        sales: undefined,
        brokerageType: undefined,
        brokeragePrice: undefined,
        inventory: undefined,
        saleCount: undefined,
        freight: undefined,
        sort: undefined,
        isRecommend: undefined,
        createTime: undefined,
        updateTime: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodsId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 显示详情 */
    showDetail(row) {
      this.openDetail = true;
      this.form = row;
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加商品";

      this.$router.push({ path: '/goods/add' });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const goodsId = row.goodsId || this.ids
      getGoods(goodsId).then(response => {
        this.form = response.data;
        // this.open = true;
        // this.title = "修改商品";

        this.$router.push({ path: '/goods/edit', query: { form: JSON.stringify(this.form) } });
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goodsId != undefined) {
            updateGoods(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addGoods(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsIds = row.goodsId || this.ids;
      this.$confirm('是否确认删除商品编号为"' + goodsIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGoods(goodsIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportGoods(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>

<style>
  .table-expand {
    font-size: 0;
  }
  .table-expand label {
    width: 100px;
    color: #99a9bf;
  }
  .table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
  }
  .banners {
    width: 80px;
    margin-right: 10px;
  }
</style>
